package org.subject.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.subject.dao.FacultyMapper;
import org.subject.dao.UserMapper;
import org.subject.dto.MessageAndUser;
import org.subject.entity.Faculty;
import org.subject.entity.Message;
import org.subject.entity.Page;
import org.subject.entity.User;
import org.subject.service.impl.FacultyServiceImpl;
import org.subject.service.impl.MessageServiceImpl;
import org.subject.service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 对用户进行操作的控制器
 * Created by kai on 2017/5/1.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private FacultyServiceImpl facultyService;
    @Autowired
    private MessageServiceImpl messageService;

    private User user;

    /**
     * 获取用户详细信息
     *
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping("/detail")
    public String userDetail(Model model, @RequestParam("userId") String userId) {
        user = userService.selectByPrimaryKey(userId);
        model.addAttribute("user", user);
        return "person";
    }

    /**
     * 修改个人信息
     *
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping(value = "/edit")
    public String userEdit(Model model, @RequestParam("userId") String userId) {
        user = userService.selectByPrimaryKey(userId);
        model.addAttribute("user", user);
        return "editperson";
    }

    /**
     * 获取用户注册页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/register")
    public String register(Model model) {
        User user = new User();
        model.addAttribute("user", user);
        return "register";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    public String addUser(Model model, @ModelAttribute("SpringWeb") User addUser) {
        logger.info(addUser.toString());
        if (addUser != null) {
            List<Faculty> facultyList = facultyService.selectAllFaculty();
            for (Faculty faculty : facultyList) {
                if (addUser.getUserRemark().equals(faculty.getFacultyName())) {
                    addUser.setFacultyId(faculty.getFacultyId());
                    addUser.setUserRemark(faculty.getFacultyName());
                    break;
                }
            }
            addUser.setUserRole(3);
            addUser.setUserState(String.valueOf(0));
            addUser.setCreateTime(new Date());
            userService.insertSelective(addUser);
        }
        int pages = 1;
        List<MessageAndUser> messageAndUsers = messageService.queryMessageList("", "", pages);
        model.addAttribute("list", messageAndUsers);
        int count = messageService.queryMessageCount("", "");
        Page page = new Page();
        page.setTotalNumber(count);
        model.addAttribute("page", page);
        return "index";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    public String modifyUser(@ModelAttribute("SpringWeb") User modifyUser, Model model) {
        logger.info(modifyUser.toString());
        if (modifyUser.getUserName() != null && !"".equals(modifyUser.getUserName())) {
            user.setUserName(modifyUser.getUserName());
        }
        if (modifyUser.getUserPassword() != null && !"".equals(modifyUser.getUserPassword())) {
            user.setUserPassword(modifyUser.getUserPassword());
        }
        if (modifyUser.getUserPhone() != null && !"".equals(modifyUser.getUserPhone())) {
            user.setUserPhone(modifyUser.getUserPhone());
        }
        if (modifyUser.getUserProfession() != null && !"".equals(modifyUser.getUserProfession())) {
            user.setUserProfession(modifyUser.getUserProfession());
        }
        if (modifyUser.getUserClass() != null && !"".equals(modifyUser.getUserClass())) {
            user.setUserClass(modifyUser.getUserClass());
        }
        if (modifyUser.getUserRemark() != null && !"".equals(modifyUser.getUserRemark())) {
            List<Faculty> facultyList = facultyService.selectAllFaculty();
            for (Faculty faculty : facultyList) {
                if (modifyUser.getUserRemark().equals(faculty.getFacultyName())) {
                    user.setFacultyId(faculty.getFacultyId());
                    user.setUserRemark(faculty.getFacultyName());
                    break;
                }
            }
        }
        int code = userService.updateByPrimaryKey(user);
        model.addAttribute("user", userService.selectByPrimaryKey(String.valueOf(user.getUserId())));
        return "person";
    }

    /**
     * 所有院系信息
     *
     * @return
     */
    @ModelAttribute("facultyList")
    public List<String> getWebFrameworkList() {
        List<String> list = new ArrayList<String>();
        List<Faculty> facultyList = facultyService.selectAllFaculty();
        for (Faculty faculty : facultyList) {
            list.add(faculty.getFacultyName());
        }
        return list;
    }

    /**
     * 获取性别
     *
     * @return
     */
    @ModelAttribute("sexList")
    public List<String> getSexList() {
        List<String> list = new ArrayList<String>();
        list.add("男");
        list.add("女");
        list.add("保密");
        return list;
    }
}
